//package com.haojiang.mh.ui.fragment
//
//import android.widget.TextView
//import com.haojiang.mh.AppContext
//import com.haojiang.mh.R
//import com.haojiang.mh.cache.Const
//import com.haojiang.mh.cache.MGlobal
//import com.haojiang.mh.service.api.JsonCallback
//import com.haojiang.mh.service.entity.*
//import com.haojiang.mh.service.event.CardEvent
//import com.haojiang.mh.service.event.ReportEvent
//import com.haojiang.mh.ui.activity.SleepAdviceActivity
//import com.haojiang.mh.ui.base.BaseActivity
//import com.haojiang.mh.ui.base.BaseScrollAbleFragment
//import com.haojiang.mh.utils.KLog
//import com.haojiang.mh.utils.MUtil
//import com.haojiang.mh.utils.SpUtil
//import com.haojiang.mh.widget.SleepStandardView
//import com.lzy.okgo.OkGo
//import com.lzy.okgo.model.Response
//import kotlinx.android.synthetic.main.fragment_day.*
//import kotlinx.android.synthetic.main.layout_day_body_move.*
//import kotlinx.android.synthetic.main.layout_day_breath_rate.*
//import kotlinx.android.synthetic.main.layout_day_heart_rate.*
//import kotlinx.android.synthetic.main.layout_day_sleep_advice.*
//import kotlinx.android.synthetic.main.layout_day_sleep_info.*
//import kotlinx.android.synthetic.main.layout_day_snoring_intervene.*
//import kotlinx.android.synthetic.main.layout_day_summary.*
//import org.greenrobot.eventbus.EventBus
//import org.greenrobot.eventbus.Subscribe
//import org.greenrobot.eventbus.ThreadMode
//import java.util.*
//import kotlin.collections.ArrayList
//import kotlin.collections.HashMap
//
///**
// * 日报
// *
// */
//class Day111Fragment : BaseScrollAbleFragment() {
//
//    private var currDay = ""
//    private var currHasReportData = true
//    private lateinit var unitHour: String
//    private lateinit var unitMin: String
//
//    override fun getLayoutId() = R.layout.fragment_day
//
//    override fun initView() {
//        unitHour = context.getString(R.string.f_unit_hour)
//        unitMin = context.getString(R.string.f_unit_min)
//        tv_radar.setOnClickListener { redirectExplain("day_radar") }
//        aiv_sleep_kind_mark.setOnClickListener { redirectExplain("day_sleep_kind") }
//        aiv_day_snore_mark.setOnClickListener { redirectExplain("day_snore") }
//        aiv_heart_rate_mark.setOnClickListener { redirectExplain("day_heart") }
//        aiv_breath_rate_mark.setOnClickListener { redirectExplain("day_breath") }
//        aiv_body_move_mark.setOnClickListener { redirectExplain("day_body_move") }
//        aiv_advice_bt.setOnClickListener {
//            if (currHasReportData) {
//                context.redirectWithExtra(currDay, SleepAdviceActivity::class.java)
//            } else {
//                showToast(getString(R.string.txt_no_report_advice))
//            }
//        }
//
//        //初始化最高最低文本显示
//        initHeartRateInfo(null)
//        initBreathRateInfo(null)
//    }
//
//    override fun onFragmentResume() {
//        super.onFragmentResume()
////        decideToGetDayReport()
//    }
//
//    override fun onDestroyView() {
//        OkGo.getInstance().cancelTag(this)
//        super.onDestroyView()
//    }
//
//    //region 初始化所有图表
//    private fun initAllChartView(bean: DayReportBean) {
//        calcCardInfoData(bean)
//        if (bean.data == null) return
//        //区分当前是左边还是右边的用户数据
//        val data = if (MGlobal.instance.currLeftSide) {
//            bean.data?.left
//        } else {
//            bean.data?.right
//        }
//        currHasReportData = data?.score ?: -1 >= 0
//        initRadarData(data)
//        initSleepIndex(data)
//        initSleepChartInfo(data)
//        initSnoringInfo(data)
//        initHeartRateInfo(data)
//        initBreathRateInfo(data)
//        initBodyMoveInfo(data)
//    }
//
//    /**
//     * 重置所有View
//     */
//    private fun resetAllView() {
//        if (!currHasReportData) return
//        val rb = ReportBean()
//        val drb = DayReportBean()
//        drb.data = rb
//        initAllChartView(drb)
//    }
//    //endregion
//
//    //region 计算卡片数据
//    private fun calcCardInfoData(bean: DayReportBean) {
//        if (bean.data == null) return
//        val infoList = ArrayList<CardInfoBean>()
//        val left = CardInfoBean()
//        if (bean.data?.left != null) {
//            left.score = bean.data?.left?.score ?: -1
//            if (left.score == -1) {
//                left.evaluate = -1
//            } else {
//                val move = bean.data?.left?.body_move_times ?: -1
//                val revolve = bean.data?.left?.body_revolve_times ?: -1
//                left.evaluate = move + revolve
//            }
//            left.type = 0
//        }
//        infoList.add(left)
//        val right = CardInfoBean()
//        if (bean.data?.right != null) {
//            right.score = bean.data?.right?.score ?: -1
//            if (right.score == -1) {
//                right.evaluate = -1
//            } else {
//                val move = bean.data?.right?.body_move_times ?: -1
//                val revolve = bean.data?.right?.body_revolve_times ?: -1
//                right.evaluate = move + revolve
//            }
//            right.type = 0
//        }
//        infoList.add(right)
//        EventBus.getDefault().post(CardEvent(infoList))
//    }
//    //endregion
//
//    //region 雷达图和指标相关
//    private fun initRadarData(bean: SideBean?) {
//        radar_day.setValues(
//                (bean?.summary_chart?.deep ?: -1) * 0.01f,
//                (bean?.summary_chart?.rem ?: -1) * 0.01f,
//                (bean?.summary_chart?.snore_count ?: -1) * 0.01f,
//                (bean?.summary_chart?.sleep_during_time ?: -1) * 0.01f,
//                (bean?.summary_chart?.away_bed ?: -1) * 0.01f,
//                (bean?.summary_chart?.asleep_time ?: -1) * 0.01f
//        )
//    }
//
//    /**
//     * 四个指标
//     */
//    private fun initSleepIndex(bean: SideBean?) {
//        val subReport = bean?.sub_report?.get(0)
//        calcSleepTime(subReport)
//        val h = subReport?.sleep_time_hour ?: -1
//        val m = subReport?.sleep_time_minute ?: -1
//        setFormatText(tv_sleep_duration, h * 60 + m, 0)
//        setFormatText(tv_go_bed, subReport?.go_to_bed_time ?: -1L, 3)
//        setFormatText(tv_on_bed, subReport?.wake_up_time ?: -1L, 3)
//    }
//
//    /**
//     * 计算入睡时长
//     */
//    private fun calcSleepTime(subReport: SubReportBean?) {
//        if (subReport?.sleep_data.isNullOrEmpty()) {
//            tv_sleep_time.text = "--"
//        } else {
//            val start = subReport?.sleep_data!![0].start
//            var end = 0L
//            for (data in subReport.sleep_data!!) {
//                if (data.status in 1..3) {
//                    end = data.end
//                    break
//                }
//            }
//            if (end == 0L) end = start //这种情况等同于没睡 给默认值 0
//            setFormatText(tv_sleep_time, (end - start).toInt() / 60, 0)
//        }
//    }
//    //endregion
//
//    //region 深浅睡眠详细
//    private fun initSleepChartInfo(bean: SideBean?) {
//        val deep = bean?.sub_report?.get(0)?.deep
//        val light = bean?.sub_report?.get(0)?.light
//        val rem = bean?.sub_report?.get(0)?.rem
//        val awake = bean?.sub_report?.get(0)?.awake
//        val curr = bean?.sub_report?.get(0)?.sleep_data
//        ssv_deep_sleep.setSleepTimeAndType(deep?.value ?: -1, SleepStandardView.DEEP_SLEEP)
//        setFormatText(tv_deep_sleep_duration, deep?.value ?: -1, 0)
//        tv_deep_sleep_percent.text = if (deep?.percentage ?: -1 > 0) "${deep?.percentage}%" else "0%"
//
//        ssv_light_sleep.setSleepTimeAndType(light?.value ?: -1, SleepStandardView.LIGHT_SLEEP)
//        setFormatText(tv_light_sleep_duration, light?.value ?: -1, 0)
//        tv_light_sleep_percent.text = if (light?.percentage ?: -1 > 0) "${light?.percentage}%" else "0%"
//
//        ssv_rem.setSleepTimeAndType(rem?.value ?: -1, SleepStandardView.REM)
//        setFormatText(tv_rem_duration, rem?.value ?: -1, 0)
//        tv_rem_percent.text = if (rem?.percentage ?: -1 > 0) "${rem?.percentage}%" else "0%"
//
//        ssv_awake.setSleepTimeAndType(awake?.value ?: -1, SleepStandardView.AWAKE)
//        setFormatText(tv_awake_duration, awake?.value ?: -1, 0)
//        tv_awake_percent.text = if (awake?.percentage ?: -1 > 0) "${awake?.percentage}%" else "0%"
//
//        if (curr.isNullOrEmpty()) dsc_day.showNoData() else dsc_day.setChartDataNew(curr)
//    }
//    //endregion
//
//    //region 打鼾
//    private fun initSnoringInfo(bean: SideBean?) {
//        val currSnore = bean?.sub_report?.get(0)?.snore_count
//        val currIntervene = bean?.sub_report?.get(0)?.snore
//        val currBgData = bean?.sub_report?.get(0)?.sleep_data
//        calcSnoreCount(currSnore)
//        calcInterveneCount(currIntervene)
//        if (currSnore == null && currIntervene == null) {
//            sec_snore_rate.showNoData()
//        } else {
//            sec_snore_rate.setChartDataNew(currBgData, currSnore, currIntervene)
//        }
//    }
//
//    /**
//     * 计算打鼾次数
//     */
//    private fun calcSnoreCount(snore_count: List<TimeStampBean>?) {
//        if (snore_count == null) {
//            setFormatText(tv_snore_count, -1, 1)
//        } else {
//            var count = 0
//            for (data in snore_count) {
//                count += data.value
//            }
//            setFormatText(tv_snore_count, count, 1)
//        }
//    }
//
//    /**
//     * 计算干预次数
//     */
//    private fun calcInterveneCount(snore: List<TimeStampBean>?) {
//        if (snore == null) {
//            setFormatText(tv_interrupt_count, -1, 1)
//        } else {
//            setFormatText(tv_interrupt_count, snore.size, 1)
//        }
//    }
//    //endregion
//
//    //region 平均心率
//    private fun initHeartRateInfo(bean: SideBean?) {
//        val heart = bean?.sub_report?.get(0)?.heart_beat_avg ?: -1
//        val curr = bean?.sub_report?.get(0)?.heart_beat
//        val currBgData = bean?.sub_report?.get(0)?.sleep_data
//        val highest: String
//        val lowest: String
//        setFormatText(tv_avg_heart_rate, heart, 1)
//        if (curr == null || curr.isNull()) {
//            sec_heart_rate.showNoData()
//            highest = "--"
//            lowest = "--"
//        } else {
//            val sort = curr.num!!.sorted()//取最高最低心率
//            highest = "${sort[sort.size - 1]}"
//            lowest = "${sort[0]}"
//            val newList = ArrayList<TimeValueBean>()
//            for (index in curr.time!!.indices) {
//                val tvb = TimeValueBean()
//                tvb.time = String.format(Locale.getDefault(), "%02d:00", curr.time!![index])
//                tvb.value = curr.num!![index]
//                newList.add(tvb)
//            }
//            sec_heart_rate.setChartDataNew(currBgData, newList)
//        }
//        tv_max_heart_rate.text = getString(R.string.tv_highest, highest, getString(R.string.tv_heart_rate_unit))
//        tv_min_heart_rate.text = getString(R.string.tv_lowest, lowest, getString(R.string.tv_heart_rate_unit))
//    }
//    //endregion
//
//    //region 平均呼吸频率
//    private fun initBreathRateInfo(bean: SideBean?) {
//        val breath = bean?.sub_report?.get(0)?.breath_avg ?: -1
//        setFormatText(tv_breath_rate, breath, 1)
//        val curr = bean?.sub_report?.get(0)?.breath
//        val currBgData = bean?.sub_report?.get(0)?.sleep_data
//        val highest: String
//        val lowest: String
//        if (curr == null || curr.isNull()) {
//            sec_breath.showNoData()
//            highest = "--"
//            lowest = "--"
//        } else {
//            //取最高最低呼吸
//            val sort = curr.num!!.sorted()
//            highest = "${sort[sort.size - 1]}"
//            lowest = "${sort[0]}"
//            val newList = ArrayList<TimeValueBean>()
//            for (index in curr.time!!.indices) {
//                val tvb = TimeValueBean()
//                tvb.time = String.format(Locale.getDefault(), "%02d:00", curr.time!![index])
//                tvb.value = curr.num!![index]
//                newList.add(tvb)
//            }
//            sec_breath.setChartDataNew(currBgData, newList)
//        }
//        tv_max_breath_rate.text = getString(R.string.tv_highest, highest, getString(R.string.tv_avg_unit))
//        tv_min_breath_rate.text = getString(R.string.tv_lowest, lowest, getString(R.string.tv_avg_unit))
//    }
//    //endregion
//
//    //region 体动和翻身
//    private fun initBodyMoveInfo(bean: SideBean?) {
//        val move = bean?.body_move_times ?: -1
//        val revolve = bean?.body_revolve_times ?: -1
//        val currMove = bean?.sub_report?.get(0)?.body_move
//        val currRevolve = bean?.sub_report?.get(0)?.body_revolve
//        val currBgData = bean?.sub_report?.get(0)?.sleep_data
//        setFormatText(tv_body_move, move, 1)
//        setFormatText(tv_body_revolve, revolve, 1)
//        if (currMove.isNullOrEmpty() && currRevolve.isNullOrEmpty()) {
//            bmc_body_move.showNoData()
//        } else {
//            val newMove = ArrayList<TimeValueBean>()
//            val newRevolve = ArrayList<TimeValueBean>()
//            if (!currMove.isNullOrEmpty()) {
//                for (data in currMove) {
//                    val tvb = TimeValueBean()
//                    tvb.value = data.value
//                    tvb.time = MUtil.getHM(data.time)
//                    newMove.add(tvb)
//                }
//            }
//            if (!currRevolve.isNullOrEmpty()) {
//                for (data in currRevolve) {
//                    val tvb = TimeValueBean()
//                    tvb.value = data.value
//                    tvb.time = MUtil.getHM(data.time)
//                    newRevolve.add(tvb)
//                }
//            }
//            bmc_body_move.setChartDataNew(currBgData, newMove, newRevolve)
//        }
//    }
//    //endregion
//
//    //region 公共方法
//
//    /**
//     * 设置文本
//     */
//    private fun setFormatText(tv: TextView, obj: Any, type: Int) {
//        var text = ""
//        when (type) {
//            0 -> {//int数据显示时长 大于60分钟显示xHy格式 小于60分钟显示x min格式
//                val value = obj as Int
//
////                text = if (value < 0) "--" else if (value >= 60) "${value / 60}h${value % 60}" else "${value}min"
//                text = if (value < 0) "--" else if (value >= 60) "${value / 60}${unitHour}${value % 60}${unitMin}" else "${value}${unitMin}"
//            }
//            1 -> {//int数据显示文本
//                val value = obj as Int
//                text = if (value < 0) "--" else "$value"
//            }
//            2 -> {//String数据显示文本
//                val str = obj as String
//                text = if (str.isEmpty()) "--" else str
//            }
//            3 -> {//Long数据显示时长 大于60分钟显示xHy格式 小于60分钟显示x min格式
//                val value = obj as Long
//                text = if (value < 0L) "--" else MUtil.getHM(value)
//            }
//        }
//        tv.text = text
//    }
//
//    //endregion
//
//    //region 接口数据
//
//    /**
//     * 根据设备MAC地址和是否需要更新Token,决定去获取报告
//     */
//    private fun decideToGetDayReport() {
//        if (SpUtil[Const.DEVICE_WIFI_MAC, ""].isEmpty()) {
//            showToast(getString(R.string.text_no_device_tip))
//            hideLoadingDialog()
//            resetAllView()
//        } else {
//            context.showLoadingDialog()
//            if (SpUtil.isNeedGetToken()) {
////                refreshToken()
//                context.refreshToken(object:BaseActivity.OnRefreshTokenSuccessListener{
//                    override fun onRefreshTokenSuccess() {
//                        getSelectDayReport()
//                    }
//
//                })
//            } else {
//                getSelectDayReport()
//            }
//        }
//    }
//
//    /**
//     * 获取日报
//     */
//    private fun getSelectDayReport() {
//        val url = Const.getDayReport() + SpUtil[Const.DEVICE_WIFI_MAC, ""]
//        val queryMap = HashMap<String, String>()
//        queryMap["day"] = currDay
//        OkGo.get<DayReportBean>(url).tag(this).params(queryMap).execute(object : JsonCallback<DayReportBean>() {
//
//            override fun onNormalSuccess(response: Response<DayReportBean>) {
//                if (response.body().error.isEmpty()) {
//                    initAllChartView(response.body())
//                } else {
//                    showToast(getString(R.string.tip_get_data_fail))
//                    KLog.e("getSelectDayReport失败 response: $response")
//                    if ("token_expired" == response.body().error) {
//                        KLog.e("Token 无效")
//                    }
//                }
//            }
//
//            override fun onError(response: Response<DayReportBean>?) {
//                showToast(getString(R.string.tip_get_data_fail))
//                KLog.e("response: $response")
//            }
//
//            override fun onFinish() {
//                hideLoadingDialog()
//            }
//        })
//    }
//
//    /**
//     * 刷新Token
//     */
////    private fun refreshToken() {
////        val url = Const.getToken() + SpUtil[Const.DEVICE_WIFI_MAC, ""]
////        val queryMap = HashMap<String, String>()
////        queryMap["access_key"] = Const.ACCESS_KEY
////        queryMap["secret_key"] = Const.SECRET_KEY
////        OkGo.get<TokenBean>(url).tag(this).params(queryMap).execute(object : JsonCallback<TokenBean>() {
////
////            override fun onNormalSuccess(response: Response<TokenBean>) {
////                if (response.body().error.isEmpty()) {
////                    SpUtil.put(Const.TOKEN_LOCAL, response.body().data?.token ?: "")
////                    SpUtil.put(Const.TOKEN_EXIST_TIME, System.currentTimeMillis())
////                    getSelectDayReport()
////                } else {
////                    hideLoadingDialog()
////                    KLog.e("Token 更新失败 response: $response")
////                    when (response.body().error) {
////                        "key_error" -> KLog.e("公私钥错误")
////                        "mac_error" -> KLog.e("MAC地址格式错误")
////                    }
////                }
////            }
////
////            override fun onError(response: Response<TokenBean>?) {
////                hideLoadingDialog()
////                KLog.e("Token 更新失败 response: $response")
////            }
////        })
////    }
//    //endregion
//
//    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
//    fun onReceiveEvent(event: ReportEvent) {
//        if (event.type != Const.DAY_OPERATE) return
//        if (event.paramMap.isNullOrEmpty()) return
//        val year = event.paramMap["year"] as Int
//        val month = event.paramMap["month"] as Int
//        val day = event.paramMap["day"] as Int
//        currDay = String.format(Locale.getDefault(), "%04d-%02d-%02d", year, month, day)
//        decideToGetDayReport()
//    }
//}